package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.TAppDto;
import com.ruoyi.system.domain.dto.TAppInterfaceDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.TAppVo;
import com.ruoyi.system.domain.vo.TInterfaceVo;
import com.ruoyi.system.mapper.TAppInterfaceMapper;
import com.ruoyi.system.mapper.TAppMapper;
import com.ruoyi.system.mapper.TInterfaceMapper;
import com.ruoyi.system.service.TAppService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TAppServiceImpl implements TAppService {
    
    @Resource
    TAppMapper appMapper;
    @Resource
    TAppInterfaceMapper appInterfaceMapper;
    @Resource
    TInterfaceMapper interfaceMapper;
    
    @Override
    public Integer insertApp(TAppDto.Insert appInsert) {
        // 新增实体数据
        TApp tApp = BeanUtil.copyProperties(appInsert, TApp.class);
        long appId = IdUtil.getSnowflake().nextId();
        tApp.setId(appId);
        tApp.setCreateBy(SecurityUtils.getUsername());
        tApp.setCreateTime(new Date());
        if (CollectionUtil.isNotEmpty(appInsert.getInterfaceIdList())) {
            // 新增关联关系
            for (Long interfaceId : appInsert.getInterfaceIdList()) {
                TAppInterface appInterface = new TAppInterface();
                appInterface.setInterfaceId(interfaceId);
                appInterface.setAppId(appId);
                appInterface.setCreateBy(SecurityUtils.getUsername());
                appInterface.setCreateTime(new Date());
                appInterfaceMapper.insertSelective(appInterface);
            }
        }
        return appMapper.insertSelective(tApp);
    }

    @Override
    public Integer updateApp(TAppDto.Update appUpdate) {
        // 修改实体
        TApp tApp = BeanUtil.copyProperties(appUpdate, TApp.class);

        TAppDto.Update update = BeanUtil.copyProperties(tApp, TAppDto.Update.class);
        tApp.setUpdateBy(SecurityUtils.getUsername());
        tApp.setUpdateTime(new Date());
        return appMapper.updateByPrimaryKeySelective(tApp);
    }

    @Override
    public Integer deleteApp(TAppDto.Delete appDelete) {
        TAppExample interfaceExample = new TAppExample();
        interfaceExample.createCriteria().andIdIn(appDelete.getIdList());
        TApp tApp = new TApp();
        tApp.setDelFlag((byte) 2);
        return appMapper.updateByExampleSelective(tApp, interfaceExample);
    }

    @Override
    public List<TAppVo> listApp(TAppDto.Query queryApp) {
        TAppExample appExample = new TAppExample();
        TAppExample.Criteria criteria = appExample.createCriteria().andDelFlagEqualTo((byte)0);
        if (StrUtil.isNotEmpty(queryApp.getAppId())) {
            criteria.andAppIdEqualTo(queryApp.getAppId());
        }
        if (StrUtil.isNotEmpty(queryApp.getAppName())) {
            criteria.andAppNameLike("%" + queryApp.getAppName() + "%");
        }
        if (StrUtil.isNotEmpty(queryApp.getAppKey())) {
            criteria.andAppKeyLike("%" + queryApp.getAppKey() + "%");
        }
        if (StrUtil.isNotEmpty(queryApp.getContract())) {
            criteria.andContractLike("%" + queryApp.getContract() + "%");
        }
        if (StrUtil.isNotEmpty(queryApp.getCompanyName())) {
            criteria.andCompanyNameLike("%" + queryApp.getCompanyName() + "%");
        }
        appExample.setOrderByClause("create_time desc");
        List<TApp> appList = appMapper.selectByExample(appExample);
        return appList.stream().map(e -> BeanUtil.copyProperties(e, TAppVo.class)).collect(Collectors.toList());
    }

    @Override
    public Integer insertAppInterface(TAppInterfaceDto.Insert appInterfaceInsert) {
        int result = 0;
        for (Long interfaceId : appInterfaceInsert.getInterfaceIdList()) {
            TAppInterface appInterface = new TAppInterface();
            appInterface.setInterfaceId(interfaceId);
            appInterface.setAppId(appInterfaceInsert.getAppId());
            appInterface.setFrequency("-1");
            appInterface.setCreateBy(SecurityUtils.getUsername());
            appInterface.setFrequency(appInterfaceInsert.getFrequency());
            appInterface.setCreateTime(new Date());
            result += appInterfaceMapper.insertSelective(appInterface);
        }
        return result;
    }

    @Override
    public Integer updateAppInterface(TAppInterfaceDto.Update appInterfaceUpdate) {
        TAppInterface appInterface = new TAppInterface();
        appInterface.setFrequency(appInterfaceUpdate.getFrequency());
        appInterface.setUpdateBy(SecurityUtils.getUsername());
        appInterface.setUpdateTime(new Date());
        TAppInterfaceExample appInterfaceExample = new TAppInterfaceExample();
        appInterfaceExample.createCriteria().andInterfaceIdEqualTo(appInterfaceUpdate.getInterfaceId()).andAppIdEqualTo(appInterfaceUpdate.getAppId());
        return appInterfaceMapper.updateByExampleSelective(appInterface, appInterfaceExample);
    }

    @Override
    public Integer deleteAppInterface(TAppInterfaceDto.Delete appInterfaceDelete) {
        int result = 0;
        TAppInterface appInterface = new TAppInterface();
        appInterface.setDelFlag((byte)2);
        appInterface.setCreateBy(SecurityUtils.getUsername());
        appInterface.setCreateTime(new Date());
        for (Long interfaceId : appInterfaceDelete.getInterfaceIdList()) {
            TAppInterfaceExample appInterfaceExample = new TAppInterfaceExample();
            appInterfaceExample.createCriteria().andInterfaceIdEqualTo(interfaceId).andAppIdEqualTo(appInterfaceDelete.getAppId());
            result += appInterfaceMapper.updateByExampleSelective(appInterface, appInterfaceExample);
        }
        return result;
    }

    @Override
    public List<TInterfaceVo> listAppInterface(TAppInterfaceDto.Query queryApp) {
        List<TAppInterface> appInterfaceList = appInterfaceMapper.selectByExample(new TAppInterfaceExample() {{
            createCriteria().andAppIdEqualTo(queryApp.getAppId()).andDelFlagEqualTo((byte) 0);
        }});
        if (CollectionUtil.isNotEmpty(appInterfaceList)) {
            List<Long> interfaceIdList = appInterfaceList.stream().map(TAppInterface::getInterfaceId).collect(Collectors.toList());
            TInterfaceExample interfaceExample = new TInterfaceExample();
            interfaceExample.createCriteria().andIdIn(interfaceIdList).andDelFlagEqualTo((byte) 0);
            interfaceExample.setOrderByClause("sort asc, create_time desc");
            List<TInterface> interfaceList = interfaceMapper.selectByExample(interfaceExample);
            return interfaceList.stream().map(e -> BeanUtil.copyProperties(e, TInterfaceVo.class)).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
